﻿using UnityEngine;
using System.Collections;

/// <summary>
/// Smartfox Network Handler for Join.
/// </summary>
[AddComponentMenu("wsLibrary/Network/Smartfox Join Handler")]
public class wsSmartfoxJoinHandler : wsSmartfoxNetClient
{
	public string roomName;
	public string roomPass;
	public bool createRoom;
	public bool autoJoin = false;

	public delegate void SNetEventRoom(Sfs2X.Entities.Room room);
	public event SNetEventRoom OnCreate = delegate { };
	public event SNetEventRoom OnJoin = delegate { };

	public void Join(string roomName, string roomPass, bool createRoom)
	{
		smartfoxManager.JoinRoom(roomName, roomPass, createRoom);
	}

	protected override void Init()
	{
		if (autoJoin)
		{
			wsBaseUser player = wsSmartfoxUserManager.Instance.GetPlayer();
			roomName += "_" + player.userInfo.Name;
			Join(roomName, roomPass, createRoom);
		}
	}

	protected override void OnCreateRoomEvent(Sfs2X.Entities.Room room)
	{
		OnCreate(room);
	}

	protected override void OnJoinRoomEvent(Sfs2X.Entities.Room room)
	{
		//this.Log("JoinHandler> Joined in successfully, room: " + room);
		OnJoin(room);
	}
}
